package com.udi.project.business.base.controller;

import com.udi.common.constant.UserConstants;
import com.udi.common.utils.StringUtils;
import com.udi.framework.web.controller.BaseController;
import com.udi.framework.web.domain.AjaxResult;
import com.udi.project.business.base.domain.FlCode;
import com.udi.project.business.base.domain.Ttree;
import com.udi.project.business.base.service.FlCodeService;
import com.udi.project.system.dept.domain.Dept;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/base_")
public class FlcodeController extends BaseController {
    private String per = "base/flcode";

    @Autowired
    FlCodeService flCodeService;

    @RequiresPermissions("base:flcode:view")
    @GetMapping("/flcode")
    public String list(){
        return per + "/main";
    }

    /**
     * 新增分类编码视图
     */
    @GetMapping("/add_view/{Id}")
    public String add(@PathVariable("Id") Integer Id, ModelMap mmap)
    {
        mmap.put("flcode", flCodeService.selectflCodeById(Id));
        return per + "/add";
    }

    /**
     * 编辑分类编码视图
     */
    @GetMapping("/edit_view/{Id}")
    public String edit(@PathVariable("Id") Integer Id, ModelMap mmap)
    {
        FlCode flCode = flCodeService.selectflCodeById(Id);
        /*if (StringUtils.isNotNull(flCode) && (2 == Id ||1 == Id))
        {
            flCode.setFlName("无");
        }*/
        mmap.put("flcode", flCode);
        return per + "/edit";
    }
    /**
     * 选择分类编码树视图
     *
     * @param flId 分类编码ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectFlCodeTree/{flId}", "/selectFlCodeTree/{flId}/{excludeId}" })
    public String selectFlCodeTree(@PathVariable("flId") Integer flId, @PathVariable(value = "excludeId", required = false) String excludeId, ModelMap mmap)
    {
        mmap.put("flcode", flCodeService.selectflCodeById(flId));
        mmap.put("excludeId", excludeId);
        return per + "/tree";
    }

    /**
     * 加载分类编码列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ttree> treeData()
    {
        List<Ttree> ttrees = flCodeService.selecflCodeTree(new FlCode());
        return ttrees;
    }

    /**
     * 加载分类编码列表树（排除下级）
     */
    @GetMapping("/treeData/{excludeId}")
    @ResponseBody
    public List<Ttree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Integer excludeId)
    {
        FlCode flCode = new FlCode();
        flCode.setFlId(excludeId);
        List<Ttree> ttrees = flCodeService.selecflCodeTreeExcludeChild(flCode);
        return ttrees;
    }




    /**
     *
     * @param flCode 列表数据
     [flCode]* @return java.util.List<com.udi.project.business.base.domain.FlCode>
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/19 15:35
     */
    @RequiresPermissions("base:flcode:list")
    @RequestMapping("/list")
    @ResponseBody
    public List<FlCode> main(FlCode flCode){
        List<FlCode> flCodeList = flCodeService.selectFlCodeList(flCode);
        return flCodeList;
    }

    @RequiresPermissions("base:flcode:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FlCode flCode)
    {
        /*if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
        {
            return error("新增分类编码'" + dept.getDeptName() + "'失败，分类编码名称已存在");
        }*/
        return toAjax(flCodeService.insertFlCode(flCode));
    }

    @RequiresPermissions("base:flcode:del")
    @GetMapping("/remove/{Id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("Id") Integer flId)
    {
        System.out.println("---------->"+flId);
        System.out.println(flCodeService.selectFlCodeCount(flId));
        if (flCodeService.selectFlCodeCount(flId) > 0)
        {
            return AjaxResult.warn("存在子节点,不允许删除！");
        }
        return toAjax(flCodeService.deleteFlCodeById(flId));
    }


    @RequiresPermissions("base:flcode:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated FlCode flCode)
    {
        if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(flCodeService.checkflNameUnique(flCode)))
        {
            return error("修改分类编码'" + flCode.getFlName() + "'失败，分类编码名称已存在");
        }
        else if (flCode.getParentId().equals(flCode.getFlId()))
        {
            return error("修改分类编码'" + flCode.getFlName() + "'失败，上级分类编码不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, flCode.getStatus())
                && flCodeService.selectNormalChildrenFlcodeById(flCode.getFlId()) > 0)
        {
            return AjaxResult.error("该分类编码包含未停用的子分类编码！");
        }
        return toAjax(flCodeService.updateFlcode(flCode));
    }


    /**
     * 校验同级下的分类编码名称是否一致
     */
    @PostMapping("/checkflNameUnique")
    @ResponseBody
    public String checkDeptNameUnique(FlCode flCode)
    {
        return flCodeService.checkflNameUnique(flCode);
    }
}
